package com.huiquan.symptom.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.symptom.service.SymptomService;
import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;

@Controller
public class SymptomController {

	@Autowired
	private SymptomService symptomService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/symptom/symptom_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		if (StringUtils.isBlank(tabid)) {
			tabid = (String) pageParam.get("tabid");
		}
		return symptomService.list(Integer.parseInt(start), keyword, exactSearchFlag, tabid, countPerPageStr);
	}

	/**
	 * 得到展现词列表
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/symptom/reveal_list.htm")
	@SuppressWarnings("unchecked")
	public ModelAndView revealList(HttpServletRequest req, HttpServletResponse resp) {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (StringUtils.isNotEmpty(tabid)) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
			tabid = (String) pageParam.get("tabid");
		}
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String status = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "status");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		Map<String, Object> map = symptomService.revealList(Integer.parseInt(start), keyword, exactSearchFlag, status,
				tabid, countPerPageStr);
		return new ModelAndView("symptom/index", map);
	}

	/**
	 * 编辑显示词
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/symptom/reveal_edit.do")
	@ResponseBody
	public ReturnData editReveal(HttpServletRequest req, HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("user");
		int id = Integer.valueOf(req.getParameter("id"));
		String symptomArray = req.getParameter("symptomArray");
		String propArray = req.getParameter("propArray");
		return symptomService.editReveal(user, id, symptomArray, propArray);
	}

	/**
	 * 更新显示词状态
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/symptom/reveal_update_status.do")
	@ResponseBody
	public ReturnData updateRevealStatus(HttpServletRequest req, HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("user");
		int id = Integer.valueOf(req.getParameter("id"));
		int revealStatus = Integer.valueOf(req.getParameter("revealStatus"));
		return symptomService.updateRevealStatus(user, id, revealStatus);
	}

	/**
	 * 根据选择的同义词改变展现词
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/symptom/reveal_change_with_synonym.do")
	@ResponseBody
	public ReturnData changeRevealWithSynonym(HttpServletRequest req, HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("user");
		int id = Integer.valueOf(req.getParameter("id"));
		String symptomStd = req.getParameter("symptomStd");
		String synonym = req.getParameter("synonym");
		return symptomService.changeRevealWithSynonym(user, id, symptomStd, synonym);
	}

	@RequestMapping(value = "/symptom/symptom_pre_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		String keyword = req.getParameter("keyword");
		String id = req.getParameter("id");
		User user = (User) req.getSession().getAttribute("user");
		Integer status = RequestParamUtil.getIntegerDefaultDi(req, "status", 0);
		return symptomService.preEdit(keyword, start, id, user, status);
	}

	@ResponseBody
	@RequestMapping(value = "/symptom/symptom_edit.do")
	public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		Long id = Long.parseLong(req.getParameter("id"));
		String name = req.getParameter("name");
		String list = req.getParameter("list");
		String attribution = req.getParameter("attribution");
		String blacklistFlag = req.getParameter("blacklistFlag");
		return symptomService.edit(id, name, list, attribution, blacklistFlag, user);
	}

	@RequestMapping(value = "/symptom/symptom_del.htm")
	public ModelAndView delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		@SuppressWarnings("unchecked")
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		Long id = Long.parseLong(RequestParamUtil.getParamByRequestOrSession(req, pageParam, "id"));
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String tabid = (String) pageParam.get("tabid");
		return symptomService.delete(Integer.parseInt(start), id, keyword, exactSearchFlag, tabid, countPerPageStr);
	}

	@RequestMapping(value = "/symptom/symptom_del_blacklist.htm")
	public ModelAndView delBlacklist(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		@SuppressWarnings("unchecked")
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String id = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "id");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		User user = (User) req.getSession().getAttribute("user");
		String tabid = (String) pageParam.get("tabid");
		return symptomService.delBlacklist(Integer.parseInt(start), id, keyword, exactSearchFlag, tabid, user,
				countPerPageStr);
	}

	@RequestMapping(value = "/symptom/symptom_add_blacklist.htm")
	public ModelAndView addBlacklist(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		@SuppressWarnings("unchecked")
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String id = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "id");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		User user = (User) req.getSession().getAttribute("user");
		String tabid = (String) pageParam.get("tabid");
		return symptomService.addBlacklist(Integer.parseInt(start), id, keyword, exactSearchFlag, tabid, user,
				countPerPageStr);
	}
}
